﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XHRPMIS.Business;
using EFFC.Frame.Net.Base.Data.Base;
using EFFC.Frame.Net.Module.Extend.EWRA;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Base.Data;
using ICPFrameLib.Business;

namespace XHRPMIS
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var options = FrameDLRObject.CreateInstance();
            //EWRA自定义模块加载
            options.RestAPIModuleName = typeof(MyWebModule).FullName;
            //Logic参数设定
            options.RestAPILogicAssemblyName = "XHRPMIS";
            //RestAPI模块启用定义
            options.RestAPILogicBaseType = typeof(MyRestLogic);
            //Logic Plugin设定
            options.RestAPILoginPluginPath = "Plugin";
            //Tag模块启动定义
            options.TagAssembly = "XHRPMIS";
            //默认起始路由
            options.DefaultStartRoute = MyConfig.GetConfiguration("Server", "DefaultStartRoute");
            //设置apidoc的路由
            options.APIDocRoute = MyConfig.GetConfiguration("Server", "APIDocRoute");
            //设置是否显示api doc
            options.IsShowRestAPIDoc = BoolStd.IsNotBoolThen(MyConfig.GetConfiguration("Server", "IsShowAPIDoc"), false);
            //设置中间件参数
            options.MiddleWareOptionsType = typeof(MyWebOptions);
            //Mqtt Client Plugin设定
            options.MqttClientPluginPath = "Plugin";

        //websocket设置
        var is_ws_active = BoolStd.IsNotBoolThen(MyConfig.GetConfiguration("WebSocket", "IsActive"));
            var keep_alive_interval = IntStd.IsNotIntThen(MyConfig.GetConfiguration("WebSocket", "KeepAliveInterval"), -1);
            var receive_buffer_size = IntStd.IsNotIntThen(MyConfig.GetConfiguration("WebSocket", "ReceiveBufferSize"), -1);
            string allowed_origins = ComFunc.nvl(MyConfig.GetConfiguration("WebSocket", "AllowedOrigins"));
            if (is_ws_active)
            {
                var ws_op = new WebSocketOptions();
                if (keep_alive_interval > 0)
                {
                    ws_op.KeepAliveInterval = TimeSpan.FromSeconds(keep_alive_interval);
                }
                if (receive_buffer_size > 0)
                {
                    ws_op.ReceiveBufferSize = receive_buffer_size;
                }
                if (allowed_origins != "" && allowed_origins != "*")
                {
                    var arr = allowed_origins.Split(",");
                    if (!arr.Contains("*"))
                    {
                        foreach (var origins in arr)
                        {
                            ws_op.AllowedOrigins.Add(origins);
                        }
                    }
                }

                app.UseWebSockets(ws_op).UseEWRAWSMiddleWare((FrameDLRObject)options);
            }
            else
            {
                app.UseEWRAMiddleWare((FrameDLRObject)options);
            }
        }
    }
}
